; Copyright (c) 2009 Michael Streif
;
; Permission is hereby granted, free of charge, to any person obtaining a copy
; of this software and associated documentation files (the "Software"), to deal
; in the Software without restriction, including without limitation the rights
; to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
; copies of the Software, and to permit persons to whom the Software is
; furnished to do so, subject to the following conditions:
;
; The above copyright notice and this permission notice shall be included in
; all copies or substantial portions of the Software.
;
; THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
; IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
; FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
; AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
; LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
; OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
; THE SOFTWARE.


BITS 64

global enableINTS
global disableINTS
global loadIDT

;Exception stubs
global exep0
global exep1
global exep2
global exep3
global exep4
global exep5
global exep6
global exep7
global exep8
global exep9
global exep10
global exep11
global exep12
global exep13
global exep14
global exep15
global exep16
global exep17
global exep18
global exep19

global irq1
global test

extern generalHandler
extern keyboardIRQ

enableINTS:
	sti
	ret

disableINTS:
	cli
	ret

loadIDT:
	lidt [rdi]
	ret

%macro HANDLER 1
	exep%1:

	push qword 0		;yeah, we don't have an error code here!
	push qword %1	      ;push exep number!

	PUSH_REGS

	mov rdi, rsp		;pass the whole interrupt state to the handler

	push rsp
	mov rsp, failsafeStack
	call generalHandler
	pop rsp

	POP_REGS

	add rsp, 16			;remove exep num and err code from stack
	iretq
%endmacro

%macro HANDLER_ERRCODE 1
	exep%1:

	push qword %1	      ;push exep number!

	PUSH_REGS

	mov rdi, rsp		;pass the whole interrupt state to the handler

	push rsp
	mov rsp, failsafeStack
	call generalHandler
	pop rsp

	POP_REGS

	add rsp, 16			;remove exep num and err code from stack
	iretq
%endmacro

%macro PUSH_REGS 0
	push rax
	push rbx
	push rcx
	push rdx
	push rdi
	push rsi
	push rbp

	push r8
	push r9
	push r10
	push r11
	push r12
	push r13
	push r14
	push r15

	push fs
	push gs
%endmacro

%macro POP_REGS 0
	pop gs
	pop fs

	pop r15
	pop r14
	pop r13
	pop r12
	pop r11
	pop r10
	pop r9
	pop r8

	pop rbp
	pop rsi
	pop rdi
	pop rdx
	pop rcx
	pop rbx
	pop rax
%endmacro

HANDLER 0
HANDLER 1
HANDLER 2
HANDLER 3
HANDLER 4
HANDLER 5
HANDLER 6
HANDLER 7
HANDLER_ERRCODE 8
HANDLER 9
HANDLER_ERRCODE 10
HANDLER_ERRCODE 11
HANDLER_ERRCODE 12
HANDLER_ERRCODE 13
HANDLER_ERRCODE 14
HANDLER 15
HANDLER 16
HANDLER_ERRCODE 17
HANDLER 18
HANDLER 19

irq1:
	cli
	PUSH_REGS

	;cld
	;mov rdi, rsp		;pass the whole interrupt state to the handler

	call keyboardIRQ

	;hlt
	POP_REGS

	;add rsp, 8
	iretq

test:
	cli
	PUSH_REGS

	;cld
	;mov rdi, rsp		;pass the whole interrupt state to the handler

	;call testHandler

	POP_REGS


	iretq

section .bss

resb 1000
failsafeStack:
